home *** CD-ROM | disk | FTP | other *** search
/ ADA Programming Guide / ADA Programming Guide.iso / ada_gwu / axqr.c < prev    next >
C/C++ Source or Header  |  1996-01-30  |  6KB  |  192 lines

  1. /*
  2.  * Copyright (C) 1985-1992  New York University
  3.  * 
  4.  * This file is part of the Ada/Ed-C system.  See the Ada/Ed README file for
  5.  * warranty (none) and distribution info and also the GNU General Public
  6.  * License for more details.
  7.  
  8.  */
  9.  
  10. /* procedures for reading axq files.
  11.  * Since these are used by the interpreter, it is ESSENTIAL that
  12.  * neither hdr.h or ghdr.h be required for their compilation.
  13.  */
  14.  
  15. #include "config.h"
  16. #include <stdio.h>
  17. #include "ifile.h"
  18. #include "segment.h"
  19. #include "slot.h"
  20. #include "stdlib.h"
  21. #include "libfp.h"
  22. #include "miscp.h"
  23. #include "axqrp.h"
  24.  
  25. static Slot *get_slot(IFILE *, int, int *);
  26.  
  27. Segment segment_new(int kind, int initdim)        /*;segment_new*/
  28. {
  29.     Segment    s;
  30.  
  31.     s = (Segment) emalloct(sizeof(Segment_s), "segment-new");
  32.     s->seg_kind = kind;
  33.     s->seg_ptr = (int **) 0;
  34.     if (kind==SEGMENT_KIND_CODE) {
  35.         s->seg_size = sizeof(char);
  36.         if (initdim==0) initdim = 128;
  37.         s->seg_dim = initdim;
  38.         s->seg_extend = 128;
  39.     }
  40.     else {
  41.         s->seg_size = sizeof(int);
  42.         if (initdim==0) initdim = 32; /* initial dimension */
  43.         s->seg_dim = initdim;
  44.         s->seg_extend = 8;
  45.     }
  46.     s->seg_pos = 0;
  47.     s->seg_maxpos = 0;
  48.     s->seg_data = emalloct(s->seg_dim * s->seg_size, "segment-data");
  49.     s->seg_id = SEG_ID;        /* insert value to check segment validity*/
  50.     return s;
  51. }
  52.  
  53. Segment segment_read(IFILE *file)                /*;segment_read*/
  54. {
  55.     Segment    seg;
  56.     short    dim;
  57.     int        kind;
  58.     kind = getnum(file,"segment-kind");
  59.     dim = getnum(file,"segment-dim");
  60.     seg = segment_new(kind, dim);
  61.     fread(seg->seg_data,seg->seg_size,dim,file->fh_file);
  62.     seg->seg_maxpos = dim - 1;
  63.     return seg;
  64. }
  65.  
  66. void read_axq(IFILE *axq_file, Axq axq)                            /*;read_axq*/
  67. {
  68.     /* axq has been set with loads an AXQ */
  69.  
  70.     Segment    newseg,segment_get();
  71.     int        si,snum,nsegs,sj;
  72.     char    **get_axq_slots();
  73.     char    *funame,*u_type;
  74.     long    genpos,rec;
  75.  
  76. #ifdef TBSL
  77.     if (debug_trace) {
  78.         TO_ERRFILE("reading axq ",  axq_file_name) ;
  79.     }
  80.     efclose(axq_file);
  81. #endif
  82.     for (rec=read_init(axq_file); rec != 0; rec=read_next(axq_file,rec)) {
  83.         funame = getstr(axq_file,"axq-unit-name");
  84.         getnum(axq_file,"axq-num");
  85.         u_type = unit_name_type(funame);
  86.         /* In the C version, DATA_SLOTS CODE_SLOTS and EXCEPTION_SLOTS are at
  87.          * the end of the file, and are read below
  88.          */
  89.         /* load subprogram bodies, package specs and bodies and main unit */
  90.         if (!(streq(u_type,"su") || streq(u_type,"sp") || streq(u_type,"bo")
  91.           || streq(u_type,"ma"))) {
  92.             continue; /* skip other units */
  93.         }
  94.         genpos = getlong(axq_file,"axq-gen-pos");
  95.         /* position to start of generator info */
  96.         ifseek(axq_file, "axq-gen_info", genpos, 0);
  97.         for (si=0;si<4;si++) {
  98.             nsegs = getnum(axq_file,"number-segments");
  99.             for (sj=1;sj<=nsegs;sj++) {
  100.                 snum = getnum(axq_file,"axq-segment-num");
  101.                 newseg = segment_read(axq_file);
  102.                 if (newseg->seg_kind==SEGMENT_KIND_CODE) {
  103.                     if (snum>=axq->axq_code_segments_dim)  {
  104.                         chaos("code_segment_number error");
  105.                     }
  106.                     axq->axq_code_segments[snum] = newseg->seg_data;
  107.                     axq->axq_code_seglen[snum] = newseg->seg_dim;
  108.                 }
  109.                 if (newseg->seg_kind==SEGMENT_KIND_DATA) {
  110.                     if (snum>=axq->axq_data_segments_dim)  {
  111.                         chaos("data_segment number too big");
  112.                     }
  113.                     axq->axq_data_segments[snum] = (int *) newseg->seg_data;
  114.                     axq->axq_data_seglen[snum] = newseg->seg_dim;
  115.                 }
  116.             }
  117.         }
  118.     }
  119. #ifdef TBSL
  120. else {
  121.     TO_LIST("*** Unable to read AXQfile ",axq_file_name);
  122.     stop;
  123. }
  124. #endif
  125. }
  126.  
  127. long get_cde_slots(IFILE *file, Axq axq)                    /*;get_cde_slots*/
  128. {
  129.     /* get slots info from file. The offset within the file is not changed */
  130.  
  131.     long    dpos,start_pos;
  132.     int        n_code,n_data,n_exception,dim;
  133.  
  134.     start_pos = iftell(file); /* save pos so can restore after reading slots*/
  135.     dpos = file->fh_slots;/* get offset for slots */
  136.     if (dpos==0l)
  137.         chaos("get_cde_slots: slot offset zero");
  138.     ifseek(file,"slots-start",dpos,0);/* position to start of slot info */
  139.     n_code = getnum(file,"n-code");
  140.     n_data = getnum(file,"n-data");
  141.     n_exception = getnum(file,"n-exception");
  142.     axq->axq_code_slots_dim = n_code + 1;
  143.     axq->axq_code_slots = get_slot(file,n_code,&dim);
  144.     axq->axq_code_segments_dim = dim;
  145.     axq->axq_data_slots_dim = n_data + 1;
  146.     axq->axq_data_slots = get_slot(file,n_data,&dim);
  147.     axq->axq_data_segments_dim = dim;
  148.     axq->axq_exception_slots_dim = n_exception + 1;
  149.     axq->axq_exception_slots = get_slot(file,n_exception,&dim);
  150.     axq->axq_exception_slots_dim = dim;
  151.     ifseek(file,"slots-reset",start_pos,0);/* restore position */
  152.     return dpos; /* return offset of start of slot info */
  153. }
  154.  
  155. static Slot *get_slot(IFILE *file, int nmax, int *dim)            /*;get_slot*/
  156. {
  157.     /* This procedure reads in the SLOTS information. 
  158.      * Entries are Slots structures. nmax is guess at needed dimension,
  159.      * dim is set to dimension actually found.
  160.      */
  161.  
  162.     int i,n,exists;
  163.     Slot    *tup;
  164.     Slot    slot;
  165.  
  166.     *dim = nmax+1;
  167.     tup = (Slot *) emalloct((unsigned)*dim * sizeof(Slot), "get-slot");
  168.     n  = getnum(file,"slot-entries");
  169.     for (i = 1; i <= n; i++) {
  170.         exists = getnum(file,"slot-exists");
  171.         if (exists) {
  172.             slot = (Slot) emalloct(sizeof(Slot_s), "get-slot-1");
  173.             slot->slot_seq = getnum(file,"slot-seq");
  174.             slot->slot_unit = getnum(file,"slot-unit");
  175.             slot->slot_number = getnum(file,"slot-number");
  176.             slot->slot_name = getstr(file,"slot_name");
  177. #ifdef MONITOR
  178.             slot->slot_file = getstr(file,"slot_file");
  179.             slot->slot_package = getstr(file,"slot_package");
  180. #endif
  181.             if (slot->slot_number >= *dim) {
  182.                 *dim = slot->slot_number + 1;
  183.             }
  184.             tup[i] =  slot;
  185.         }
  186.         else {
  187.             tup[i] = (Slot)0;
  188.         }
  189.     }
  190.     return tup;
  191. }
  192.